package com.example.module_detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.MovieVideoEntity

import com.example.detail.databinding.ItemDetailMovieLayoutBinding
import com.example.image.ImgOptions
import com.example.image.LoadImageUtil

/**
 * Author: Jason Zhang
 * Date: 2025/2/11
 * Desc:
 */
class DetailMoviePagingAdapter: PagingDataAdapter<MovieVideoEntity, DetailMoviePagingAdapter.DetailViewHolder>(
    MovieDetailEntityComparator
) {

    private var itemClickListener: ItemClickListener? = null

    fun setItemClickListener(listener: ItemClickListener) {
        itemClickListener = listener
    }

    inner class DetailViewHolder(val dataBind: ItemDetailMovieLayoutBinding) : RecyclerView.ViewHolder(dataBind.root)

    override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
        val entity = getItem(position)
        if (entity != null){
            LoadImageUtil.loadUrl(holder.dataBind.root.context, entity.videomainimag,  holder.dataBind.ivMovieImg, ImgOptions.Builder.isCircle(false).build())
            holder.dataBind.tvMovieDesc.text = entity.description
            holder.dataBind.tvPlayCount.text = "${entity.favoritecount + 321}次播放"

            holder.dataBind.root.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position, entity)
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
        return DetailViewHolder(ItemDetailMovieLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    interface ItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity)
    }


}